package com.jhj.common.http;

import com.jhj.common.util.JhjFileOperator;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 发送HTTP请求(仅在包内使用)
 * 
 * @author GaoXiang Date: 2014-9-30
 */
class JhjSender {
    
    private static JhjSender instance = new JhjSender();
    
    private JhjFileOperator fileOperator = JhjFileOperator.getInstance();
    
    private JhjSender() {
    }
    
    static JhjSender getInstance() {
        return instance;
    }
    
    /**
     * 发送http请求
     * 
     * @param httpUrl
     * @return
     * @throws IOException
     */
    String sendHttpRequest(String httpUrl) throws IOException {
        StringBuffer sb = new StringBuffer();
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            URL url = new URL(httpUrl);
            URLConnection urlConnection = url.openConnection();
            urlConnection.setAllowUserInteraction(false);
            isr = new InputStreamReader(url.openStream(), "utf-8");
            br = new BufferedReader(isr);
            
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            fileOperator.closeResources(isr, br);
        }
        
        return sb.toString();
    }
    
    /**
     * 利用URL发起POST请求，并接收返回信息
     * 
     * @param url 请求URL
     * @param params 请求参数
     * @return 响应内容
     * @throws IOException
     */
    String sendPostRequest(String url, Map<String, String> params) throws IOException {
        StringBuffer result = new StringBuffer();
        StringBuffer paramString = new StringBuffer();
        OutputStream os = null;
        DataOutputStream dos = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            URL urls = new URL(url);
            HttpURLConnection uc = (HttpURLConnection) urls.openConnection();
            uc.setRequestMethod("POST");
            uc.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            uc.setRequestProperty("charset", "UTF-8");
            uc.setDoOutput(true);
            uc.setDoInput(true);
            uc.setReadTimeout(10000);
            uc.setConnectTimeout(10000);
            os = uc.getOutputStream();
            dos = new DataOutputStream(os);
            
            // 组装参数
            for (Entry<String, String> entry : params.entrySet()) {
                paramString.append(entry.getKey());
                paramString.append("=");
                paramString.append(entry.getValue());
                paramString.append("&");
            }
            // 去掉末尾的&
            String param = paramString.length() > 0 ? paramString.substring(0, paramString.length() - 1) : paramString.toString();
            dos.write(param.getBytes("utf-8"));
            dos.flush();
            isr = new InputStreamReader(uc.getInputStream(), "utf-8");
            br = new BufferedReader(isr);
            String readLine = "";
            while ((readLine = br.readLine()) != null) {
                result.append(readLine);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            fileOperator.closeResources(os, dos, isr, br);
        }
        
        return result.toString();
    }
}
